package com.dylan.datacollection;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

import com.dylan.datacollection.adapter.MsgListAdapter;
import com.dylan.datacollection.adapter.RankRecordListAdapter;
import com.dylan.datacollection.db.SharedPreferencesUtils;
import com.dylan.datacollection.entity.JCEntity;
import com.dylan.datacollection.entity.MsgEntity;
import com.dylan.datacollection.net.HttpConfig;
import com.dylan.datacollection.net.HttpHelper;
import com.dylan.datacollection.net.ResponseCallback;
import com.dylan.datacollection.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RankDetailActivity extends AppCompatActivity {
    String id;
    Context context;
    TextView pjfTextView;
    TextView pmTextView;
    TextView addressTextView;
    ListView listView;
    RankRecordListAdapter rankRecordListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank_detail);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        context = this;
        id = getIntent().getStringExtra("id");
        pjfTextView = (TextView) findViewById(R.id.rank_detail_pjf);
        pmTextView = (TextView) findViewById(R.id.rank_detail_rank);
        addressTextView = (TextView) findViewById(R.id.rank_detail_address);
        listView = (ListView) findViewById(R.id.rank_detail_listView);
        getDetail(id);
        getRecordList(id);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void getRecordList(String id){
        HttpHelper.getInstance().scoreList(1,id,new ResponseCallback(){
            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                try {
                    Log.i("rank","record response:"+response.toString());
                    JSONObject jsonObject = new JSONObject(response.toString());
                    if (jsonObject.optInt("status") == HttpConfig.SUCCESS){
                        JSONArray jsonArr = jsonObject.optJSONArray("data");
                        List<JCEntity> requestArrays = new ArrayList<JCEntity>();
                        for( int i=0; i<jsonArr.length(); i++ ){
                            JSONObject jsonObj = jsonArr.getJSONObject(i);
                            Gson gson = new Gson();
                            JCEntity msgEntity = gson.fromJson(new String(jsonObj.toString()),JCEntity.class);
                            requestArrays.add(msgEntity);
                            rankRecordListAdapter = new RankRecordListAdapter(1,context,requestArrays);
                            listView.setAdapter(rankRecordListAdapter);
                        }
                    }else{
                        ToastUtils.showShortMsg(context,jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void getDetail(String id){
        HttpHelper.getInstance().jcDetail(id,new ResponseCallback(){
            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                try {
//                    Log.i("LOGIN","response:"+response.toString());
                    JSONObject jsonObject = new JSONObject(response.toString());
                    if (jsonObject.optInt("code") == HttpConfig.SUCCESS){
                        Gson gson = new Gson();
                        JCEntity jcEntity = gson.fromJson(new String(jsonObject.getJSONObject("data").toString()),JCEntity.class);
                        pjfTextView.setText(jcEntity.getPjf());
                        pmTextView.setText(jcEntity.getPm());
                        addressTextView.setText("地址："+jcEntity.getAddress());
                    }else{
                        ToastUtils.showShortMsg(context,jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
